<?php
/**
 * 楽曲管理画面コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Song extends Controller_Manage {
	private $domain;

	/**
	 * before
	 *
	 * @access  public
	 * @return  Response
	 */
	public function before() {
		parent::before();

		$this -> paths = explode('/', Input::uri());

		if (!empty($_SERVER["HTTPS"])) {
			$this -> domain = 'https://';
		} else {
			$this -> domain = 'http://';
		}
		$this -> domain .= $_SERVER["HTTP_HOST"];
	}

	/**
	 * index
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_index() {
		return Response::forge(View::forge('manage/song/index'), D_HTTP_UPDATE);
	}

	/**
	 * コメント通報一覧
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_commentreport() {
		$description = "";
		$post = Input::post();
		$get = Input::get();

		// 復活処理
		if( !empty($get['rescue']) ) {
			$query = DB::update('comment');
			$query -> set(array('del_flg' => '0') );
			$query -> where('comment_id', $get['rescue']);
			$query -> execute();
		}

		$query = DB::select(DB::expr('comment_id AS id'), DB::expr("DATE_FORMAT(comment.create_date,'%Y/%m/%d') AS 通報日"), DB::expr('word AS コメント'), DB::expr('song_title AS 曲名'), DB::expr('artist AS アーティスト'));
		$query -> from('comment');
		$query -> where('del_flg', '1');
		$query -> join('song') -> on('comment.song_id', '=', 'song.song_id');
		$query -> order_by('comment_id', 'DESC');
		$query -> limit(1000);
		$list = $query -> execute() -> as_array();

		foreach ($list as $key => $value) {
			$list[$key]['復活'] = sprintf('%s/manage/song/commentreport/?rescue=%d', $this -> domain, $list[$key]['id']);
			unset($list[$key]['id']);
		}

		$csv = Input::get('csv');
		if (empty($csv)) {
			return Response::forge(View::forge('manage/table', array('list' => $list)), D_HTTP_UPDATE);
		} else {
			return Response::forge(View::forge('manage/csv', array('list' => $list)), D_HTTP_UPDATE);
		}
	}

}
